<template>
    <div class="login-bg">
    <div class="login-contain">
    <div class="login-header">
        <p>欢迎登录</p>
    </div>
    <div class="form-group" v-show="from">
        <div class="form-item">
            <label for="username">
                <img src="../assets/img/user.png">
            </label>
            <input v-model="phone" id="phone" type="text" placeholder="请输入手机号">
        </div>
        <div class="form-item">
            <label for="password">
                <img src="../assets/img/password.png" alt="">
            </label>
            <input v-model="password" id="password" type="password" placeholder="请输入密码">
        </div>
    </div>
    <div class="form-group" v-show="!from">
        <div class="form-item">
            <label for="username">
                <img src="../assets/img/user.png">
            </label>
            <input v-model="phone" id="phone" type="text" placeholder="请输入手机号">
        </div>
        <div class="form-item">
            <label for="captcha">
                <img src="../assets/img/password.png" alt="">
            </label>
            <input v-model="captcha" id="captcha" placeholder="请输入验证码">
            <span><button class="get" @click="getSent">发送验证码</button></span>
        </div>
    </div>
    <div class="form-other" v-show="from">
        <div>
            忘记密码？
        </div>
        <div  class="form-other1-text" @click="from=!from">
            短信验证登录
        </div>
    </div>
    <div class="form-other" v-show="!from">
        <div></div>
        <div @click="from=!from">
            手机账号登录
        </div>
    </div>
    <div class="button-group">
        <button @click="loginEvent" class="login-btn" id="doLogin">登录</button>
    </div>
    <div class="button-group">
        <button @click="logonEvent" class="login-btn" id="doLogin">注册</button>
    </div>
    <div class="order-login">
        <p class="order-login-line">其他登录方式</p>
        <div class="order-login-box">
            <div>
                <a href="#">
                    <img src="../assets/img/wechat-login.png" alt="" style="width: 45px;height: 45px;">
                    <p>微信登录</p>
                </a>
            </div>
        </div>
    </div>
</div>
</div>
</template>
<script>
import { setSent } from '../api/index'
import { Toast } from 'vant'
export default {
  data () {
    return {
      phone: '',
      password: '',
      captcha: '',
      from: true
    }
  },
  methods: {
    loginEvent: async function () {
      if (this.from) {
        const result = await this.$store.dispatch('phoneLogin', { phone: this.phone, password: encodeURIComponent(this.password) })
        if (result.data.code === 200) {
          this.$router.push('/me')
        }
      } else {
        const yzm = await setSent(this.phone, this.captcha)
        console.log(1)
        if (yzm.data.code === 200) {
          console.log(2)
          const result = await this.$store.dispatch('phoneLogins', { phone: this.phone, captcha: this.captcha })
          console.log(result)
          if (result.data.code === 200) {
            console.log(3)
            this.$router.push('/me')
          }
        }
      }
    }
  },
  mounted () {
    Toast({
      message: '您未登录!',
      position: 'top'
    })
  }
}
</script>
<style lang="less" scoped>
.login-bg {
    position: fixed;
    width: 100vw;
    height: 100vh;
    left: 0;
    top: 0;
    z-index: 1000;
    background-image: url('../assets/img/login-bg.jpg');
    color: #ffffff;
    background-size: 100% 100%;
}
.login-contain {
    width: 85%;
    margin: auto;
}
.login-header {
    padding: 5%;
}
.login-header p {
    font-size: 32px;
    color: #ffffff;
    font-weight: bold;
    text-align: center;
    letter-spacing: 2px;
    text-shadow: 0.1em 0.15em 0.1em #74C3CA
}
.form-group {
    padding: 3%;
    position: relative;
    .form-item{
        margin-top: 3%;
        padding:0 10px;
        border-radius: 20px;
        background-color: #B3DFE2;
        label img{
            width: 25px;
            position: absolute;
            margin-top: 5px;
        }
        input{
            outline: none;
            border: 0;
            background-color: transparent;
            color: #ffffff;
            height: 40px;
            font-size: 18px;
            width: 55%;
            margin-left: 12%;
        }
        span{
            .get{
                height: 40px;
                display:inline-block;
                padding: 0.1rem;
                border-radius: 18px;
                color: #ccc;
                background-color: white;
                border: none;
                position:absolute;
                right: 0.23rem;
            }
        }
    }
}
.form-other{
   display:flex;
   justify-content: space-between;
   margin: 0.2rem 0.4rem;
}
.form-group .form-item input::-webkit-input-placeholder {
    /* WebKit browsers */
    color: #ffffff;
    font-size: 16px;
}
.button-group{
    padding: 3%;
    button{
        outline: none;
        border: 0;
        width: 90%;
        height: 35px;
        border-radius: 20px;
        margin-left: 4%;
        color: #ffffff;
        font-size: 18px;
    }
    .login-btn{
        background-color: #ffffff;
        color: #74C3CA;
    }
}
.order-login {
    padding: 3%;
    .order-login-line {
        display: block;
        position: relative;
        text-align: center;
        font-size: 14px;
        color: #ffffff;
    }
    .order-login-line:before,
    .order-login-line:after {
        content: '';
        position: absolute;
        top: 50%;
        background: #ffffff;
        width: 20%;
        height: 1px;
    }
    .order-login-line:before {
        left: 10%;
    }
    .order-login-line:after {
        right: 10%;
    }
    .order-login-box {
        display: flex;
        width: 100%;
        justify-content:center;
        margin-top: 20px;
        div{
            flex: 1;
            text-align: center;
            p{
                text-align: center;
                font-size: 14px;
                color: #ffffff;
            }
        }
    }
}
</style>
